package com.ruoyi.oar.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oar.mapper.OarUserMapper;
import com.ruoyi.oar.domain.OarUser;
import com.ruoyi.oar.domain.dto.OarUserDTO;
import com.ruoyi.oar.domain.query.OarUserQuery;
import com.ruoyi.oar.service.OarUserService;
import java.util.stream.Collectors;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Optional;

/**
 * 从业人员Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Service
public class OarUserServiceImpl implements OarUserService
{
    @Autowired
    private OarUserMapper oarUserMapper;

    /**
     * 查询从业人员
     * 
     * @param id 从业人员主键
     * @return 从业人员
     */
    @Override
    public Optional<OarUserDTO> selectOarUserById(String id) {
        return oarUserMapper.selectOarUserById(id).map(oarUser -> toDto(oarUser,true));
    }

    /**
     * 查询从业人员列表
     * 
     * @param oarUserQuery 从业人员
     * @return 从业人员
     */
    @Override
    public List<OarUserDTO> selectOarUserList(OarUserQuery oarUserQuery) {
        return oarUserMapper.selectOarUserList(oarUserQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 分页查询从业人员列表
     * @param oarUserQuery 从业人员
     * @return
     */
    @Override
    public TableDataInfo<OarUserDTO> findAll(OarUserQuery oarUserQuery) {
        List<OarUser> list = oarUserMapper.selectOarUserList(oarUserQuery);
        List<OarUserDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }
    /**
     * 新增从业人员
     * 
     * @param oarUserDTO 从业人员
     * @return 结果
     */
    @Override
    public int insertOarUser(OarUserDTO oarUserDTO) {
        checkOarUser(oarUserDTO);
        return oarUserMapper.insertOarUser(toEntity(oarUserDTO));
    }

    /**
     * 修改从业人员
     * 
     * @param oarUserDTO 从业人员
     * @return 结果
     */
    @Override
    public int updateOarUser(OarUserDTO oarUserDTO) {
        checkOarUser(oarUserDTO);
        return oarUserMapper.updateOarUser(toEntity(oarUserDTO));
    }

    /**
     * 批量删除从业人员
     * 
     * @param ids 需要删除的从业人员主键
     * @return 结果
     */
    @Override
    public int deleteOarUserByIds(String[] ids) {
        return oarUserMapper.deleteOarUserByIds(ids);
    }

    /**
     * 删除从业人员信息
     * 
     * @param id 从业人员主键
     * @return 结果
     */
    @Override
    public int deleteOarUserById(String id)
    {
        return oarUserMapper.deleteOarUserById(id);
    }

    public TableDataInfo<OarUserDTO> selOarUserAndHouseVo(OarUserQuery oarUserQuery) {
        List<OarUser> list = oarUserMapper.selOarUserAndHouseVo(oarUserQuery);
        List<OarUserDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }


    /**
     * 根据房屋id查询用户
     * @param houseId
     * @return
     */
    @Override
    public List<OarUserDTO> selectOarUserByHouseId(String houseId) {
        return oarUserMapper.selectOarUserByHouseId(houseId).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 根据id查询从业人员及房屋信息列表
     * @param id
     * @return
     */
    @Override
    public Optional<OarUserDTO> selUserAndHouseById(String id) {
        return oarUserMapper.selUserAndHouseById(id).map(oarUser -> toDto(oarUser,true));
    }

    /**
    * 校验
    * @param dto
    */
    private void checkOarUser(OarUserDTO dto) {

    }

    private OarUser toEntity(OarUserDTO dto) {
        return BeanUtil.toBean(dto, OarUser.class);
    }

    private OarUserDTO toDto(OarUser entity) {
        return toDto(entity, false);
    }
    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarUserDTO toDto(OarUser entity, Boolean withDetail) {
            OarUserDTO dto =  BeanUtil.toBean(entity, OarUserDTO.class);
        if (withDetail) {

        }
        return dto;
    }
}
